package cn.hl.org.controller;

import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.PageList;
import cn.hl.org.domain.Employee;
import cn.hl.org.query.EmployeeQuery;
import cn.hl.org.service.IEmployeeService;
import cn.hl.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    /** 调service层 */
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 根据id查询员工，Get，url地址栏传参
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Employee findById(@PathVariable("id") Long id) {
        return employeeService.findById(id);
    }

    /**
     * 查询所有员工，Get
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * 根据id删除员工，Delete，url地址栏传参
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 添加或修改，Put，请求体传参
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addAndUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询，Post,请求体传参
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 批量删除，Patch,请求体传参
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：根据店铺id查询店主和店员
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    public List<Employee> findShopById(@PathVariable("id") Long id){
        return employeeService.findByShopId(id);
    }
}
